package com.xxx.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xxx.controller.BaseController;
import com.xxx.controller.utils.R;
import com.xxx.pojo.Product;
import com.xxx.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {
    @Autowired
    private ProductService productService;

    //显示热门商品信息功能
    @RequestMapping("/getHotProducts")
    public R getHotProducts(){
        List<Product> result = productService.getHotProducts();
        return new R(OK,result);
    }

    //查看单个商品详细信息功能
    @RequestMapping("/getOneProduct/{id}")
    public R getOneProduct(@PathVariable Integer id){
        System.out.println("=====>"+id);
        Product product = productService.getOneProduct(id);
        return new R(OK,product);
    }

    private static final int PAGE_SIZE = 6;
    //根据条件获取 商品列表 模糊查询
    @RequestMapping("/getProductsByCondition")
    public R getProductsByCondition(Integer currentPage,String condition){
        if(!condition.isEmpty()){
            IPage<Product> productsByCondition = productService.getProductsByCondition(currentPage, PAGE_SIZE, condition);
            return new R(OK,productsByCondition);
        }
        return new R(OK);
    }

    //获取新上架商品功能
    @RequestMapping("/getNewProducts")
    public R getNewProducts(){
        List<Product> result = productService.getNewProducts();
        return new R(OK,result);
    }
}
